﻿using AutoMapper;
using ManYan.Application.VO;
using ManYan.Common;
using ManYan.Domain.Model.Entity;
using ManYan.Repository.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Ocsp;

namespace ManYan.Api.Controllers
{
    [AllowAnonymous]
    [Route("api/[controller]")]
    [ApiController]
    public class WebSearchController : ControllerBase
    {
        private IBaseRepository<tb_exam_content> _examRepository;
        private IBaseRepository<tb_system_nav> _sysMenuRepository;
        private IMapper _mapper;
        public WebSearchController(IBaseRepository<tb_exam_content> examRepository, IBaseRepository<tb_system_nav> sysMenuRepository, IMapper mapper)
        {
            this._mapper = mapper;
            _examRepository = examRepository;
            _sysMenuRepository = sysMenuRepository;
        }
        [HttpGet]
        public Res GetHotExam()
        {
            var query = from ta in _examRepository.LoadEntityAll().AsNoTracking()
                        join tb in _sysMenuRepository.LoadEntityAll().AsNoTracking() on ta.CateId equals tb.Id
                        where ta.IsHot == true && ta.IsDeleted == false
                        select new ExamContentListDto() { CategoryName = tb.NavName, Title = ta.Title, Id = ta.Id, CreateTime = ta.CreatDateTime };
            var res = query.Take(100).OrderByDescending(x => x.CreateTime).ToList();
            return new Res() { Code = 200, Msg = "获取数据成功", Data = res }; ;


            // var list = _examRepository.WhereLoadEntityEnumerable(x => x.IsHot == true).Take(5).OrderByDescending(x => x.CreatDateTime).ToList();
            //   var resData = _mapper.Map<List<ExamContentListDto>>(list);
            // return new Res() { Code = 200, Msg = "获取数据成功", Data = resData };

        }
        [HttpGet("GetTopicDetail")]
        public Res GetTopicDetail(Guid id)
        {

            var query = from ta in _examRepository.LoadEntityAll().AsNoTracking()
                        join tb in _sysMenuRepository.LoadEntityAll().AsNoTracking() on ta.CateId equals tb.Id
                        where ta.Id==id && ta.IsHot == true && ta.IsDeleted == false 
                        select new  { CategoryName = tb.NavName, Title = ta.Title, Id = ta.Id, CreateTime = ta.CreatDateTime,content=ta.Content,author=ta.Author,source=ta.Source };
            return new Res() { Data = query.ToList(), Code = 200, Msg = "数据获取成功" };
        }
    }
}
